package com.hoas.keystore.factory.aes;

import com.hoas.keystore.decoder.IDecoder;
import com.hoas.keystore.util.AESUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

import static com.hoas.keystore.util.AESUtil.decrypt;
import static com.hoas.keystore.util.AESUtil.encrypt;
import static com.hoas.keystore.util.Base64Util.decryptBASE64;
import static com.hoas.keystore.util.Base64Util.encryptBASE64;

public class AESDecoder implements IDecoder, Serializable {

    private static Logger logger = LoggerFactory.getLogger(AESDecoder.class);

    private String keyType;

    private Long merchantId;

    private String version;


    private String privateKeyStr;


    private String encryptText;

    public AESDecoder(String privateKey) throws Exception {

        this.privateKeyStr = privateKey;

    }

    /**
     * 解码数据
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String decryptBody(String data, String saltKey) throws Exception {

        if (data == null || "".equals(data)) {
            return "";
        }


        String str = new String(decrypt(decryptBASE64(data), privateKeyStr,saltKey),"UTF-8");
        logger.info("get decrypt request:" + str);

        return str;


    }

    /**
     * 编码数据
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String encryptBody(String data, String saltKey) throws Exception {
        logger.info("get encoding response:" + data);

        String e = encryptBASE64(encrypt(data.getBytes("UTF-8"), privateKeyStr, saltKey));
        return e;
    }

    /**
     * 解码数据
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String decryptBody(String data) throws Exception {
        return decryptBody(data, null);
    }

    /**
     * 编码数据
     *
     * @param data
     * @return
     * @throws Exception
     */
    public String encryptBody(String data) throws Exception {
        return encryptBody(data, null);
    }

    public String sign(String str) {

        return AESUtil.sign(str.getBytes());

    }

    public boolean verify(String data, String signstr) {

        logger.info("merchantId {}, signstr => {}",merchantId, signstr);

        if(signstr == null) {
            return false;
        }

        boolean flag = AESUtil.verify( data.getBytes(), signstr);

        if ( flag ) {
            logger.info("merchantId {}, verify sign result => {}",merchantId, flag);
        } else {
            logger.error("merchantId {}, verify sign result => {}",merchantId, flag);
        }

        return flag;
    }


    public String getKeyType() {
        return keyType;
    }

    public void setKeyType(String keyType) {
        this.keyType = keyType;
    }

    public Long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(Long merchantId) {
        this.merchantId = merchantId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getPrivateKeyStr() {
        return privateKeyStr;
    }

    public void setPrivateKeyStr(String privateKeyStr) {
        this.privateKeyStr = privateKeyStr;
    }

    public String getEncryptText() {
        return encryptText;
    }

    public void setEncryptText(String encryptText) {
        this.encryptText = encryptText;
    }
}
